<?php

/**
 * login actions.
 *
 * @package    Manager
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
	public function executeIndex(sfWebRequest $request)
	{
		if(!$this->getUser()->hasCredential("admin")){
			$this->form = new LoginForm();

			if ($request->isMethod('post'))
			{
				$login = $request->getParameter('login');
				$this->form->bind($login);
				if ($this->form->isValid())
				{
					// authenticate user and redirect them
					$id = $login['username'];
					$pass = $login['password'];
					$user = Doctrine_Core::getTable("User")->findByLoginId($id);
					if(count($user) != 0 && $pass == $user[0]->getPassword()){
						$this->getUser()->setAttribute('user',$user);
						$this->getUser()->setAttribute('id',$id);
						$this->getUser()->setAttribute('name',$user[0]->getName());
						$this->getUser()->setAttribute('email',$user[0]->getEmail());
						$this->getUser()->setAuthenticated(true);
						$this->getUser()->addCredential('admin');
						$this->redirect('product/index');
					}

					else{
							
					}
				}
			}
		}else{
			$this->redirect('product/index');
		}
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->form = new UserForm();
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new UserForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
		$this->form = new UserForm($user);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
		$this->form = new UserForm($user);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		$request->checkCSRFProtection();

		$this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
		$user->delete();

		$this->redirect('login/index');
	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid())
		{
			$user = $form->save();

			$this->redirect('@homepage');
		}
	}
	public function executeLogout()
	{
		$this->getUser()->clearCredentials();
		$this->getUser()->setAuthenticated(false);
		$this->redirect('@homepage');
	}
}
